/**
 * A custom implementation of the JJTree generated
 * Node class, for storing comments.
 * 
 * @author James Stanier
 */
public class SpecialNode implements Node {

	protected Node parent;
	protected Node[] children;
	protected int id;
	protected JavaParser parser;
	protected String type;
	protected String contents;
	protected int lineNumber;

	/**
	 * Constructor.
	 * @param t The type of the comment.
	 */
	public SpecialNode(String t) {
		type = t;
	}

	/**
	 * Constructor.
	 * @param i The id number of the comment.
	 * @param l The line number that the comment starts on.
	 */
	public SpecialNode(int i, int l) {
		id = i;
		lineNumber = l;
	}

	/**
	 * Constructor.
	 * @param p A reference to the parser.
	 * @param i The id number of the comment.
	 * @param l The line number that the comment starts on.
	 */
	public SpecialNode(JavaParser p, int i, int l) {
		this(i, l);
		parser = p;
		lineNumber = l;
	}

	/**
	 * Set the contents of the comment.
	 * @param s The new comment contents.
	 */
	public void setContents(String s) {
		contents = s;
	}

	/**
	 * Get the contents of the comment.
	 * @return The comment contents.
	 */
	public String getContents() {
		return contents;
	}

	/**
	 * Method inherited from Node, but not used
	 * here.
	 */
	public void jjtOpen() {
	}

	/**
	 * Method inherited from Node, but not used
	 * here.
	 */
	public void jjtClose() {
	}

	/**
	 * Inherited method for setting the parent of this
	 * node.
	 */
	public void jjtSetParent(Node n) { parent = n; }
	
	/**
	 * Inherited method for getting the parent node.
	 */
	public Node jjtGetParent() { return parent; }

	/**
	 * Inherited method for adding a child to this node.
	 */
	public void jjtAddChild(Node n, int i) {
		if (children == null) {
			children = new Node[i + 1];
		} else if (i >= children.length) {
			Node c[] = new Node[i + 1];
			System.arraycopy(children, 0, c, 0, children.length);
			children = c;
		}
		children[i] = n;
	}

	/**
	 * Inherited method for getting the child at position i
	 * associated with this node.
	 */
	public Node jjtGetChild(int i) {
		return children[i];
	}

	/**
	 * Return the children array.
	 */
	public Node[] jjtGetChildren() {
		return children;
	}

	/**
	 * Get the number of children.
	 */
	public int jjtGetNumChildren() {
		return (children == null) ? 0 : children.length;
	}

	/**
	 * Get the line number of this comment.
	 */
	public int getLineNumber() {
		return lineNumber;
	}

	/**
	 * Get the id number of this comment.
	 * @return The comment id number.
	 */
	public int getId() {
		return id;
	}

	/* You can override these two methods in subclasses of SimpleNode to
	     customize the way the node appears when the tree is dumped.  If
	     your output uses more than one line you should override
	     toString(String), otherwise overriding toString() is probably all
	     you need to do. */

	public String toString() { return JavaParserTreeConstants.jjtNodeName[id]; }
	public String toString(String prefix) { return prefix + toString(); }

	/* Override this method if you want to customize how the node dumps
	     out its children. */

	public void dump(String prefix) {
		System.out.println(toString(prefix));
		if (children != null) {
			for (int i = 0; i < children.length; ++i) {
				Node n = (Node)children[i];
				if (n != null) {
					n.dump(prefix + " ");
				}
			}
		}
	}

	/**
	 * Method for traversing along comment nodes only, creating tokens
	 * out of them while doing so.
	 */
	public void traverseForInDepthComments(Node m) {
		SpecialNode sn = (SpecialNode)m;
		if(sn.getId() == 110) {
			CommentToken ct = new CommentToken(sn, sn.jjtGetParent(), sn.getLineNumber());
			parser.addToInDepthComments(ct);
		}
		if(children != null) {
			for(int i = 0; i < children.length; ++i) {
				Node n = (Node)children[i];
				if(n != null) {
					n.traverseForInDepthComments(n);
				}
			}
		}
	}

}